<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{total}} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">

        <!-- :default-sort="{prop:'workNumber',order:'descending'}" 是开启排序, 
        workNumber 工作时间,order 顺序 descending 降序-->
        
           <!-- prop:'timeOfEntry'  想要谁排序 这里就写谁的prop  ,另外还要在要排序的身上sortable -->
          <el-table border :data="list" :default-sort="{prop:'timeOfEntry',order:'descending'}">
         
          <el-table-column label="序号" sortable type="index"/>
          <el-table-column label="姓名" sortable prop="username"/>
          <el-table-column label="工号" sortable prop="workNumber"/>

          <el-table-column label="聘用形式"  prop="formOfEmployment">
            <template #default="{row}">
              {{hireType[row.formOfEmployment]}}
            </template>
          </el-table-column>

          <el-table-column label="部门"  prop="departmentName"/>
          <el-table-column label="入职时间" sortable prop="timeOfEntry"/>
          <el-table-column label="账户状态" />
          <el-table-column label="操作" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination layout="prev, pager, next" />
        </el-row>
      </el-card>
    </div>
  </div>
</template>
<script>
  import { getEmployeeList } from '@/api/employees'
  import  employeesEnum  from "@/constant/employees";    //这是默认导出,(自己一不小心搞成按需了  已修改)
  //3 利用reduce函数  获取数组中的元素,转换成对象   acc[item.id]=item.value  ==>1:'正式'
 const hireType= employeesEnum.hireType.reduce((acc,item)=>{
   acc[item.id]=item.value
   return acc
  },{})
export default {
  data(){
    return{
      hireType,
      list:[],  //存放员工列表
      page:1,
      size:'',
      total:0
    }
  },
  created(){
   this.loadEmployeeList()
  },
  methods:{
      
    // 获取员工列表
    async loadEmployeeList(){
        try {
        const res = await getEmployeeList()   //这里的page和size是上面data中定义的变量
        console.log('getEmployeeList', res)  //res.data.rows  /res.code
        if(res.code!==10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 保存员工数据
        this.list=res.data.rows
        // 保存员工总数
       this.total=res.data.total
      } catch (err) {
        console.log('getEmployeeList', err)
      }
    },
  }
}
</script>